<?php
namespace app\www\controller;
use x\Container;
class Goods extends Base
{
    //产品列表
    public function lists()
    {
        $d = $this->get();
        $page = $this->get('page',1);
        $limit = 20;
        $map = [];
        $map[] = ["and", "status", "=", 0];
        if (isset($d["keyword"]) && $d["keyword"]!='')     $map[] = ["and", "title", "like", '%' . $d["keyword"] . '%'];
        if (isset($d["min_price"]) && $d["min_price"]!='') $map[] = ["and", "price", ">", $d["min_price"]];
        if (isset($d["max_price"]) && $d["max_price"]!='') $map[] = ["and", "price", "<", $d["max_price"]];

        if (isset($d["cat_id"]))
        {
            $ids   = m("GoodsCat", "c")->getAllchildCat($d["cat_id"]);
            if($ids){
              $map[] = ["and", "cat_id", "in", $ids];
            }
        }
        //商品列表
        $w = where($map);
        $o = $this->get('order');
        switch ($o) {
            case 1: //综合排序
                $order = "order by sort desc,id desc";
                break;
            case 2: //销量排序
                $order = "order by sales_volume desc,sort desc";
                break;
            case 3: //人气排序
                $order = "order by views desc,sales_volume desc";
                break;
            case 4://价格升序
                $order = "order by price desc,views desc";
                break;
            case 5://价格降序
                $order = "order by price asc,views desc";
                break;
            default:
                $order = "order by sort desc,id desc";
        }
        $sql = "select * from goods $w[0] {$order} " . limit($page, $limit);
        $goods_list = $this->db->query($sql, $w[1], '', true);
        //商品列表推荐位5条数据
        $rec_goods_list = $this->db->query("select * from goods where rec_id like '%11%' and status = 0 order by sort limit 5");
        $list = [
            "goods_list" => $goods_list[0],
            "page_count" => $goods_list[1],
            "page" => $page,
            "rec_goods_list" => $rec_goods_list,
            'keyword' => isset($d['keyword']) ? $d['keyword'] : ''
        ];
        return $this->fetch($list);
    }

    function detail()
    {

        $id = $this->get('id');
        if($id){
            $rs = $this->db->query('select id from goods_price where goods_id=? order by id asc',[$id],1);
            r(url('goods/detail')."?gid=".$rs['id']);

        }
        $gid = $this->get('gid');
        //商品信息
        $info = m("goods", "c")->detail($gid);
        $info['spec'] = htmlspecialchars_decode($info['spec']);
        $info['content'] = htmlspecialchars_decode($info['content']);
        if($info['pics']){
          $info['pics'] = explode(",",$info["pics"]);
        }
        cookie("footpoint_" . $info["goods_id"], $info["goods_id"]);

        //同分类下人气商品
        $map = [
            ["and", "a.id", "!=", $info["goods_id"]],
            ["and", "b.cat_id", "=", $info["cat_id"]],
        ];
        $w = where($map);
        $sql = "
            select
                b.*,a.views
            from
                goods_price a
            left join
                goods b
            on
                a.goods_id=b.id {$w[0]}
            order by
                a.views desc
            limit
                2";
        $cat_goods = $this->db->query($sql,$w[1]);
        //人气累加
        $d = [
            "views" => $info['p_views'] + 1,
            "id" => $id
        ];
        $this->db->update("goods_price", $d);

        //属性
        $attr = [];
        $spec = json_decode($info['spec'],true);
        $ptitle = explode("_",$info['p_title']);
        $i = 0;
        foreach($spec as $k => $v){
            $spec[$k] = [];
            foreach($v as $key => $val){
                $checked = $val==$ptitle[$i] ? true:false;
                $arr = [
                    $val,
                    $checked,
                ];
                $spec[$k][] = $arr;
            }
            $i++;
        }

        //用户评论
        $sql = "
        select
            a.*,b.username
        from
            goods_evaluate a
        left join
            user b
        on
            a.user_id=b.id
        where
            a.goods_id = ?
        order by
            a.grade desc, a.id desc limit 10
        ";
        $comment = $this->db->query($sql, [$info["goods_id"]]);
        $d = [
            "goods_info"=> $info,
            "cat_goods" => $cat_goods,
            'spec'      => $spec,
            'comment' => $comment
        ];
        return $this->fetch($d);

    }

    //ajax获取商品ID
    public function findGoods(){
        $d = $this->post;
        $info = $this->db->query('select * from goods_price where title = ? and goods_id =?',[substr($d['title'],1),$d['goods_id']],1);
        cookie('js_url', url('goods/detail')."?gid=".$info['id']);
        suc();
    }

    function cat()
    {
        return $this->fetch();
    }

    public function getCategory()
    {
        $category = $this->com["categorys"];
        echo json_encode($category);
    }

    /**
     * 添加购物车
     */
    public function addCart()
    {
        $this->checkLogin();
        $d = $this->post();
        $rule = [
            'attr_id'=>'require|integer',
            'nums'=>'require|integer',
        ];
        $msg = [];
        check($rule,$msg,$d);
        $cart_count = m("goodsCart", "c")->add($d['attr_id'],$d['nums'], $this->userinfo['id']);
        if($cart_count){
            cookie('js_url', url('/user/myCart'));
            suc(["msg" => "添加购物车成功", "count" => $cart_count]);
        }else{
            err(m("goodsCart", "c")->getError());
        }
    }
}

?>
